/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package parismetro;

import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.layout.Stack;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.TextAlignment;
import javafx.scene.Group;
import modele.TplusBook;
import modele.RERBook;
import modele.RERTicket;
import modele.TplusTicket;

/**
 * @author Hadrien
 */
def f: Font = Font { size: 32, embolden: true, name: "Myriad Pro" };

public class ButtonsFare extends CustomNode {

    var coulFull: Color = Color.rgb(255, 78, 78);
    var coulReduce: Color = Color.rgb(155, 78, 78);
    var isTplus: Boolean = bind {
                (Main.orderList.orders[Main.orderList.orders.size() - 1] instanceof TplusTicket)
            }
            on replace {
                if (isTplus) {
                    coulFull = Color.GRAY;
                    coulReduce = Color.LIGHTGRAY;
                    g.disable = true;
                } else {
                    g.disable = false;
                    if (isReduce) {
                        coulReduce = Color.rgb(255, 78, 78);
                        coulFull = Color.rgb(155, 78, 78);
                    } else {
                        coulReduce = Color.rgb(155, 78, 78);
                        coulFull = Color.rgb(255, 78, 78);
                    }
                }
            }
    public var bouton_tarif_full = Stack {
                layoutX: 0;
                layoutY: 0;
                //effect: bind e;
                content: [
                    Rectangle {
                        height: 140
                        width: 140
                        arcWidth: 15, arcHeight: 15
                        stroke: bind {if (isReduce) Color.WHITE else Color.BLUE}
                        strokeWidth: 5
                        fill: bind coulFull;
                    }
                    Text {
                        font: f
                        fill: Color.WHITE
                        content: "Plein\nTarif"
                        textAlignment: TextAlignment.CENTER
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    var t =  Main.orderList.orders[Main.orderList.orders.size() - 1];
                    if (t instanceof TplusBook) {
                        (t as TplusBook).reduceFare = false;
                    } else if (t instanceof RERBook) {
                        (t as RERBook).reduceFare = false;
                    } else if (t instanceof RERTicket) {
                        (t as RERTicket).reduceFare = false;
                    }
                }
            }
    public var bouton_tarif_reduit = Stack {
                layoutX: 145;
                layoutY: 0;
                //effect: bind e;
                content: [
                    Rectangle {
                        height: 140
                        width: 140
                        arcWidth: 15, arcHeight: 15
                        stroke: bind {if (isReduce) Color.BLUE else Color.WHITE}
                        strokeWidth: 5
                        fill: bind coulReduce
                    }
                    Text {
                        fill: Color.WHITE
                        font: f
                        content: "Tarif\nRéduit"
                        textAlignment: TextAlignment.CENTER
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    var t =  Main.orderList.orders[Main.orderList.orders.size() - 1];
                    if (t instanceof TplusBook) {
                        (t as TplusBook).reduceFare = true;
                    } else if (t instanceof RERBook) {
                        (t as RERBook).reduceFare = true;
                    } else if (t instanceof RERTicket) {
                        (t as RERTicket).reduceFare = true;
                    }
                }
            }
    public var g = Group {
                content: [bouton_tarif_reduit, bouton_tarif_full]
                visible: bind not isTplus
            }
    public var isReduce: Boolean = bind {
                var t =  Main.orderList.orders[Main.orderList.orders.size() - 1];
                if (t instanceof TplusBook) {
                    (t as TplusBook).reduceFare;
                } else if (t instanceof RERBook) {
                    (t as RERBook).reduceFare;
                } else if (t instanceof RERTicket) {
                    (t as RERTicket).reduceFare;
                } else {
                    false;
                }
            }
            on replace {
                if (not isTplus) {
                    if (isReduce) {
                        coulReduce = Color.rgb(255, 78, 78);
                        coulFull = Color.rgb(155, 78, 78);
                    } else {
                        coulReduce = Color.rgb(155, 78, 78);
                        coulFull = Color.rgb(255, 78, 78);
                    }
                }
            };

    override public function create(): Node {
        return g;
    }

}
